
#import "WWInterp.h"
#import "WWArticulatedVariableBehavior.h"

@implementation WWArticulatedVariableBehavior

- initWithInterp:newInterp startTime:(float)newStartTime
                           endTime:(float)newEndTime 
                           var:(const char *)newVarName
                           expression:(const char *)newVarValueExpression
{
   interp = newInterp;
   startTime = newStartTime;
   endTime = newEndTime;
   varName = NXCopyStringBuffer(newVarName);
   varValueExpression = NXCopyStringBuffer(newVarValueExpression);
   cmd = (char *)malloc(12 + strlen(varName) + strlen(varValueExpression));
   sprintf(cmd, "set {%s} [%s]", varName, varValueExpression);

   return self;
}

- free
{
   if (varName) {  free(varName); }
   if (varValueExpression) {  free(varValueExpression); }
   if (cmd) {  free(cmd); }
   return self;
}

- (const char *)varName { return varName; }

- (const char *)varValueExpression { return varValueExpression; }

- evaluateSelfAtTime:(float)currentTime
{
   float  u;
   char   uValue[256];


   if (currentTime < startTime)
   {  return self;
   }
   if (currentTime > endTime)
   {  return self;
   }

   u = (currentTime - startTime)/(endTime - startTime);
   sprintf(uValue, "%f", u);
   [interp setVar:"u" toValue:uValue];

   if ([interp globalEval:cmd] != TCL_OK)
   {  return nil;
   }
   return self;
}

@end
